package net.tv90.bfmccore.game.view;

import lk.vexview.event.VerificationFinishEvent;
import net.tv90.bfmccore.event.FlagCapturedEvent;
import net.tv90.bfmccore.event.FlagNeutralizedEvent;
import net.tv90.bfmccore.game.GameBase;
import net.tv90.bfmccore.game.GameManager;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerRespawnEvent;

public class GameViewListener implements Listener {

    @EventHandler
    public void playerVerificationFinishEvent(VerificationFinishEvent e) {
        GameModelViewHandler.vexViewVerificationPlayers.add(e.getPlayer().getName());
    }

    @EventHandler
    public void playerQuitEvent(PlayerQuitEvent e) {
        Player p = e.getPlayer();
        GameModelViewHandler.vexViewVerificationPlayers.remove(p.getName());
    }

    @EventHandler
    public void playerConquest(FlagCapturedEvent e) {
        GameBase.firstFactionList.forEach(player -> {
            if (!GameManager.inBattleFieldPlayers.contains(player)) {
                GameModelViewHandler.getInstance().refreshMainGUI(player);
            }
        });
        GameBase.secondFactionList.forEach(player -> {
            if (!GameManager.inBattleFieldPlayers.contains(player)) {
                GameModelViewHandler.getInstance().refreshMainGUI(player);
            }
        });
    }

    @EventHandler
    public void playerNeutralize(FlagNeutralizedEvent e) {
        GameBase.firstFactionList.forEach(player -> {
            if (!GameManager.inBattleFieldPlayers.contains(player)) {
                GameModelViewHandler.getInstance().refreshMainGUI(player);
            }
        });
        GameBase.secondFactionList.forEach(player -> {
            if (!GameManager.inBattleFieldPlayers.contains(player)) {
                GameModelViewHandler.getInstance().refreshMainGUI(player);
            }
        });
    }

    @EventHandler
    public void playerRespawn(PlayerRespawnEvent e) {
        Player p = e.getPlayer();
        p.closeInventory();
        if (GameBase.firstFactionList.contains(p) || GameBase.secondFactionList.contains(p)) {
            if(!GameManager.inBattleFieldPlayers.contains(p)) {
                GameModelViewHandler.getInstance().createMainGUI(p);
            }
        }
    }
}
